<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="support.*, java.util.*, myclasses.*, java.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Provide degrees - Choose universities</title>
<script type="text/javascript" src="jquery-1.7.2.js"></script>
<script type="text/javascript">

function checkUni() {
	
	var uni = document.getElementById("uni").value;
	
	var URL = "http://localhost:8080/GradSchoolApp/uniHelpPage.jsp";
	var ret = $.getJSON(URL, "", function(data, textStatus) {
		var isEqual = false;
		for (var i = 0; i < data.length; i++) {
			if(uni == data[i].name) {
				alert("This university already exists!");
				isEqual = true;
			}		
		}
		if (!isEqual) {
			document.uniform.submit();
		}
	},"json");
	//.error(function(jqXHR, textStatus, errorThrown) { alert("error: " + textStatus + " : " + errorThrown); });
}

</script>
</head>
<body>
<%	String state = request.getParameter("stateIndex");
	Student stud = (Student)session.getAttribute("student");
	String stateName = "";
	
	
	Degree currDeg = (Degree)session.getAttribute("currDegree");
	if(currDeg == null){
		currDeg = new Degree();
		currDeg.setLocation(state);
		session.setAttribute("currDegree", currDeg);
	}
	else{
		currDeg.setLocation(state);
	}
	
	//lookup state name
	try {
		Class.forName("org.postgresql.Driver");
	} catch(Exception e) {
		out.println("org.postgresql.Driver Exception. ");
	}

	try {
		String serverName = "localhost:5432";
	    String mydatabase = "ThirdBase";
	    String url = "jdbc:postgresql://" + serverName + "/" + mydatabase; 
	    String username = "SweetAss";
	    String password = "1234";
	    Connection conn = DriverManager.getConnection(url, username, password);
	    
		Statement stmt = conn.createStatement();

		ResultSet rset = stmt.executeQuery("SELECT name FROM location where id = " + state);
		rset.next();
		stateName = rset.getString(1);
		
		rset.close();
		stmt.close();
    	conn.close();
    
	} catch(SQLException e) {
		e.printStackTrace();
		out.println("Connection exception.");
	}

%>
<center>
<h1><%=stateName %></h1>
Please choose or add your university:
</center>
<%
try {
	Class.forName("org.postgresql.Driver");
} catch(Exception e) {
	out.println("org.postgresql.Driver Exception. ");
}

try {
	String serverName = "localhost:5432";
    String mydatabase = "ThirdBase";
    String url = "jdbc:postgresql://" + serverName + "/" + mydatabase; 
    String username = "SweetAss";
    String password = "1234";
    Connection conn = DriverManager.getConnection(url, username, password);
    
	Statement stmt = conn.createStatement();

	ResultSet rset = stmt.executeQuery("SELECT id,name FROM university WHERE location = " + state);


	Integer columncounter = 0;
	out.println("<br>");
	out.println("<table width=\"600\" border=\"2\" cellpadding=\"2\" cellspacing=\"2\" align=\"center\">");

	
   	while(rset.next()) {
   		
   		columncounter += 1;
   		if(columncounter == 1)
   			out.println("<tr>");
   		
   		out.println("<td>" + "<a href=\"http://localhost:8080/GradSchoolApp/provideDegreesChooseDiscipline.jsp?uniIndex=" + rset.getString(1) + "\">" + rset.getString(2) + "</a>" + "</td>");		
   		
   		if (columncounter >= 3) {
   			out.println("</tr>");
   			columncounter = 0;
   		}
	} 
	out.println("</table>");
	out.println("<br>");
	
	rset.close();
	stmt.close();
	conn.close();

} catch(SQLException e) {
	e.printStackTrace();
	out.println("Connection exception.");
}
%>
	
	<br>
	<form name="uniform" method="GET" action="provideDegreesChooseDiscipline.jsp" onsubmit="checkUni(); return false;">
	<center>Other University: <input type="text" size="60" id="uni" name="university"/>
	<!-- send hidden state variable?-->
	<input type="submit" value="Submit"/></center>
	</form>
</body>
</html>